//
//  ReadListViewController.m
//  Leisure
//
//  Created by xalo on 16/6/29.
//  Copyright © 2016年 蓝鸥科技有限公司西安分公司. All rights reserved.
//

#import "ReadListViewController.h"

@interface ReadListViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *leftTableView;//最新列表
@property (weak, nonatomic) IBOutlet UITableView *rightTableView;//最热列表
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;//滚动视图
@property (nonatomic,strong)UISegmentedControl *segmentControl;// 分段视图
@property (nonatomic,assign)NSInteger newStart;//最新列表的起始位置
@property (nonatomic,assign)NSInteger newLimit;//最新列表的数据条数
@property (nonatomic,assign)NSInteger hotStart;//最热列表的起始位置
@property (nonatomic,assign)NSInteger hotLimit;//最热列表的数据条数

@property (nonatomic,strong)NSMutableArray *newestArray;//最新列表数据源
@property (nonatomic,strong)NSMutableArray *hotArray;//最热列表数据源

@end

@implementation ReadListViewController
-(NSMutableArray *)newestArray{
    if (!_newestArray) {
        _newestArray = [NSMutableArray array];
    }
    return _newestArray;
}

-(NSMutableArray *)hotArray{
    
    if (!_hotArray) {
        _hotArray = [NSMutableArray array];
    }
    return _hotArray;
    
}


-(UISegmentedControl *)segmentControl{
    if (!_segmentControl) {
        
        _segmentControl = [[UISegmentedControl alloc]initWithFrame:CGRectMake(0, 0, 100, 30)];
        //插入标签
        [_segmentControl insertSegmentWithTitle:@"最新" atIndex:0 animated:YES];
        [_segmentControl insertSegmentWithTitle:@"最热" atIndex:1 animated:YES];
        [_segmentControl addTarget:self action:@selector(segmentControlAction:) forControlEvents:UIControlEventValueChanged];
        
    }
    return _segmentControl;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.titleView = self.segmentControl;
    //注册Cell
    [self.leftTableView registerNib:[UINib nibWithNibName:@"ReadListCell" bundle:nil] forCellReuseIdentifier:@"ReadListModel_cell"];
    [self.rightTableView registerNib:[UINib nibWithNibName:@"ReadListCell" bundle:nil] forCellReuseIdentifier:@"ReadListModel_cell"];
    self.segmentControl.selectedSegmentIndex = 0;
    //数据请求
    [self loadNewsTableViewDataSourceWithStart:0 limit:20];
    [self loadHotTableViewDataSourceWithStart:0 limit:20];
    //添加上拉加载
    self.leftTableView.mj_footer = [MJRefreshBackGifFooter footerWithRefreshingBlock:^{
        self.newStart += 20;
        [self loadNewsTableViewDataSourceWithStart:self.newStart limit:10];
    }];
    self.rightTableView.mj_footer = [MJRefreshBackGifFooter footerWithRefreshingBlock:^{
        self.newStart += 20;
        [self loadHotTableViewDataSourceWithStart:self.newStart limit:10];
    }];
        //添加下拉刷新
    self.leftTableView.mj_header = [MJRefreshStateHeader headerWithRefreshingBlock:^{
        [self.newestArray removeAllObjects];
        [self loadNewsTableViewDataSourceWithStart:0 limit:20];
    }];
    self.rightTableView.mj_header = [MJRefreshStateHeader headerWithRefreshingBlock:^{
        [self.hotArray removeAllObjects];
        [self loadHotTableViewDataSourceWithStart:0 limit:20];
    }];
    
    
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark -----TableViewDelegate-----
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//    BaseCell *cell = [tableView dequeueReusableCellWithIdentifier:@"readList_cell" forIndexPath:indexPath];
    //创建一个model接收
    ReadListModel *model = [[ReadListModel alloc]init];
    
    if (tableView == self.leftTableView) {
        model = self.newestArray[indexPath.row];
    }else{
        model = self.hotArray[indexPath.row];
    }
    //工厂模式产出来的cell
    BaseCell *cell = [FactoryMode produceCellWithModel:model tableView:tableView indexPath:indexPath];
    NSLog(@"%@",model.title);
    [cell setCellWithModel:model];
    return cell;
    
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (tableView == self.leftTableView) {
        return self.newestArray.count;
    }else{
        return self.hotArray.count;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 200;
}
#pragma mark---tableView点击事件---
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    DetailedViewController *detailedVC = [[DetailedViewController alloc]initWithNibName:@"DetailedViewController" bundle:nil];
    if (tableView == self.leftTableView) {
        detailedVC.contentID = [self.newestArray[indexPath.row] ID];

    }else{
        detailedVC.contentID = [self.hotArray[indexPath.row] ID];
    }
    
    [self.navigationController pushViewController:detailedVC animated:YES];
    
    
    
}


#pragma mark -----分段视图控制器的点击事件-----
-(void)segmentControlAction:(UISegmentedControl *)sender{
    
    if (sender.selectedSegmentIndex == 0) {
        [self.scrollView setContentOffset:CGPointZero animated:YES];
    }else{
        [self.scrollView setContentOffset:CGPointMake(kWidth, 0) animated:YES];

    }
    
}
#pragma mark ---数据请求---

//加载最新列表的数据

-(void)loadNewsTableViewDataSourceWithStart:(NSInteger)start limit:(NSInteger)limit{
    [RequestManager fetchDataWithUrl:kReadListURl parameter:@{@"typeid":@(self.typeID),@"start":@(start),@"limit":@(limit),@"sort":@"addtime"} method:@"POST" completed:^(NSData *data)  {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
        NSLog(@"%@",dic);
        
        for (NSDictionary *dict in dic[@"data"][@"list"]) {
            ReadListModel *model = [[ReadListModel alloc]init];
            [model setValuesForKeysWithDictionary:dict];
 //加载
                [self.newestArray addObject:model];
        }
    } updateUI:^{
        NSLog(@"update");
        [self.leftTableView.mj_header endRefreshing];
        [self.leftTableView.mj_footer endRefreshing];
        [self.leftTableView reloadData];
    }];
    
    
}

//加载最热列表的数据
-(void)loadHotTableViewDataSourceWithStart:(NSInteger)start limit:(NSInteger)limit{
    [RequestManager fetchDataWithUrl:kReadListURl parameter:@{@"typeid":@(self.typeID),@"start":@(start),@"limit":@(limit),@"sort":@"hot"} method:@"POST" completed:^(NSData *data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
        NSLog(@"%@",dic);
        
        for (NSDictionary *dict in dic[@"data"][@"list"]) {
            ReadListModel *model = [[ReadListModel alloc]init];
            [model setValuesForKeysWithDictionary:dict];
            [self.hotArray addObject:model];
        }

        
    } updateUI:^{
        
        NSLog(@"update");
        [self.rightTableView.mj_header endRefreshing];
        [self.rightTableView.mj_footer endRefreshing];

        [self.rightTableView reloadData];
    }];
}


#pragma mark scrollViewDelegate
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    //先判断滚动的是不是你要滚的
    if (scrollView == self.scrollView) {
        if (self.scrollView.contentOffset.x == 0) {
            self.segmentControl.selectedSegmentIndex = 0;
        }else if (self.scrollView.contentOffset.x == kWidth){
            self.segmentControl.selectedSegmentIndex = 1;
        }
    }
}

@end
